package client.gui.panels;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;

import controllers.ControllerIF;
import controllers.TicketsTableController;
import entities.TableType;
import entities.Ticket;
import entities.TimeExtension;
import entities.User;

public class TicketsTablePanel extends JPanel implements PanelIF, MouseListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Instance of this panel's controller
	 */
	private TicketsTableController ticketsTableController;
	
	/**
	 * Private variable that states the type of table needed
	 */
	private TableType tableType;
	
	/**
	 * private variable holds all given tickets to display
	 */
	private ArrayList<Ticket> tickets;
	
	private ArrayList<User> users;
	
	private ArrayList<TimeExtension> extensions;
	
	/**
	 * Ticket object saves ticket selected by the user
	 */
	private Ticket selectedTicket;
	
	private DefaultTableModel tableModel;
	private JTable ticketsTable;
	
	
	/**
	 * Create the panel.
	 */
	public TicketsTablePanel(TableType tableType) {
		this.tableType = tableType;
		initGUI();
	}
	
	/**
	 * Initialize the UI
	 */
	public void initGUI() {
		setLayout(new BorderLayout());
		
		JPanel tablePanel = new JPanel();
		tablePanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		add(tablePanel, BorderLayout.CENTER);
		
		// Initializing the JTable as a dynamic one
		if(tableType.equals(TableType.TICKETS_LIST) || tableType.equals(TableType.SUSPEND_RESUME)) {
			String[] tableTitles = {"Ticket ID", "System Name", "Creation Date", "Ticket Description", "Ticket Status", "Ticket Stage", "Initiator ID", "Initiator Name"};
			tableModel = new DefaultTableModel(tableTitles, 0) {
				private static final long serialVersionUID = 1L;
				boolean[] columnEditables = new boolean[] {
					false, false, false, false, false, false, false, false
				};
				public boolean isCellEditable(int row, int column) {
					return columnEditables[column];
				}
			};
		}
		else if(tableType.equals(TableType.TIME_EXTENSION)) {
			String[] tableTitles = {"Ticket ID", "Applicant ID", "Current Ticket Status", "Requested Extension", "Reason"};
			tableModel = new DefaultTableModel(tableTitles, 0) {
				private static final long serialVersionUID = 1L;
				boolean[] columnEditables = new boolean[] {
					false, false, false, false, false
				};
				public boolean isCellEditable(int row, int column) {
					return columnEditables[column];
				}
			};
		}
		else if(tableType.equals(TableType.USERS_LIST)) {
			String[] tableTitles = {"User ID", "User Name", "eMail", "Phone number", "User permissions"};
			tableModel = new DefaultTableModel(tableTitles, 0) {
				private static final long serialVersionUID = 1L;
				boolean[] columnEditables = new boolean[] {
					false, false, false, false, false
				};
				public boolean isCellEditable(int row, int column) {
					return columnEditables[column];
				}
			};
		}
		
		
		ticketsTable = new JTable(tableModel);
		ticketsTable.setPreferredScrollableViewportSize(new Dimension(750, 400));
		ticketsTable.setFillsViewportHeight(true);
		ticketsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		ticketsTable.addMouseListener(this);
				
		JScrollPane tableScroll = new JScrollPane(ticketsTable);
		tablePanel.add(tableScroll);
	}

	public void fillTableData(ArrayList<Ticket> tickets) {
		this.tickets = tickets;
		for(Ticket t : tickets) {
			Object[] row = {t.getTicket_id(), t.getInfoSystem().getS_name(), t.getGeneralInfo().getInitDate(), t.getGeneralInfo().getRequestedChangedDescription(), t.getStatus(), t.getState(), t.getTicketInitiator().getId(), t.getTicketInitiator().getFirstName() + " " + t.getTicketInitiator().getLastName()};
			tableModel.addRow(row);
		}
	}
	
	public void fillTableDataWithUsers(ArrayList<User> users) {
		this.users = users;
		for(User u : users) {
			Object[] row = {u.getId(), u.getFirstName() + " " + u.getLastName(), u.getEmail(), u.getPhone(), u.getPermissions().toString()};
			tableModel.addRow(row);
		}
	}
	
	public void fillTableDataWithExtensions(ArrayList<TimeExtension> extensions) {
		this.extensions = extensions;
		for(TimeExtension te : extensions) {
			Object[] row = {te.getTicket_id(), te.getRequester_id(), te.getStatus_name(), te.getRequested_date(), te.getRequest_desc()};
			tableModel.addRow(row);
		}
	}

	@Override
	public void setController(ControllerIF cif) {
		this.ticketsTableController = (TicketsTableController) cif;
	}


	/**
	 * Get the ticket object selected by the user 
	 * @return the selectedTicket
	 */
	public Ticket getSelectedTicket() {
		return selectedTicket;
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// Only handles double click scenarios
		if(e.getClickCount() == 2) {
			if(! ticketsTable.getValueAt(1, 0).equals(null)) {
				selectedTicket = tickets.get(ticketsTable.getSelectedRow());
				ticketsTableController.action();
			}
		}
	}
	
	@Override
	public void mouseClicked(MouseEvent arg0) {}

	@Override
	public void mouseEntered(MouseEvent arg0) {}

	@Override
	public void mouseExited(MouseEvent arg0) {}

	@Override
	public void mouseReleased(MouseEvent arg0) {}

}
